#include "narrationwidgetitem.h"
#include "ui_narrationwidgetitem.h"
#include <QTextEdit>
#include "GameData/jsontranslator.h"
NarrationWidgetItem::NarrationWidgetItem(QWidget *parent,NarrationNode* n) :
    QWidget(parent),ui(new Ui::NarrationWidgetItem)
{
    ui->setupUi(this);
    Init(n);
}

NarrationWidgetItem::~NarrationWidgetItem()
{
    delete ui;
}

void NarrationWidgetItem::mousePressEvent(QMouseEvent *event)
{
    //TODO
    GameManager::Instance().ShowNarration(node);
}

void NarrationWidgetItem::Init(NarrationNode* n)
{
    ui->Condition->hide();
    ui->DataChange->hide();
    ui->DataCondition->hide();
    ui->branch->hide();
    ui->bgm->hide();
    ui->bgse->hide();
    ui->sound->hide();
    this->node = n;
    LoadNode();
    this->setDisabled(true);
}

void NarrationWidgetItem::LoadNode()
{
    if(!node)
        return;
    //富文本转为普通文本
    QTextEdit * q = new QTextEdit();
    q->setText(node->name + "\n" + node->text);
    ui->Text->setText(q->toPlainText());
    delete q;
    if(node->bgm && !node->bgm->path.isEmpty())
    {
        ui->bgm->show();
        ui->bgm->setText("播放音乐");
    }
    if(node->stopBgm)
    {
        ui->bgm->show();
        ui->bgm->setText("关闭音乐");
    }
    if(node->bgse && !node->bgse->path.isEmpty())
    {
        ui->bgse->show();
        ui->bgse->setText("播放音效");
    }
    if(node->stopBgse)
    {
        ui->bgse->show();
        ui->bgse->setText("关闭音乐");
    }
    if(node->soundPath.count() > 4)
    {
        ui->sound->show();
    }
//    if(node->options.count() > 0)
//    {
//        ui->branch->show();
//    }
    bool hasOption = false;
    for (int i = 0;i < node->linkers.count();i++)
    {
        if(node->linkers.at(i)->branchId >= 0)
        {
            hasOption = true;
            break;
        }
    }
    if(hasOption)
        ui->branch->show();
    ui->nodeName->setText(QString::number(node->id) + "："+node->nodeName);
    //LoadOptions();
}

void NarrationWidgetItem::SetName(QString s)
{
    ui->nodeName->setText(QString::number(node->id) + "："+s);
}

void NarrationWidgetItem::LoadOptions()
{
//    if(!node || node->options.count() <= 0)
//        return;
}
